package com.ywt.cda.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.ywt.cda.access.Access;
import com.ywt.cda.action.model.BaseParamModel;
import com.ywt.cda.core.dao.page.Page;
import com.ywt.cda.domain.Ana3rdData;
import com.ywt.cda.domain.AnaExportData;
import com.ywt.cda.service.ConfDataCacheService;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ywt.cda.action.model.GoodsModel;
import com.ywt.cda.action.vo.GoodsVo;
import com.ywt.cda.domain.Ana2ndData;
import com.ywt.cda.domain.AnaImportData;
import com.ywt.cda.domain.enums.DimensionFlag;
import com.ywt.cda.domain.enums.OrderByEnum;
import com.ywt.cda.domain.enums.SexEnum;
import com.ywt.cda.enums.FunctionsEnum;
import com.ywt.cda.service.AreaService;
import com.ywt.cda.service.GoodsService;
import com.ywt.cda.service.ParamValidateService;
import com.ywt.cda.service.vo.ExportGoodsDetailVo;
import com.ywt.cda.utils.DateUtils;
import com.ywt.cda.utils.NumberUtil;

/**
 * （商品分析） <br>
 * （功能详细描述）
 * 
 * @author lougf
 * @version V1.0
 * @see [相关类/方法]
 * @since 2016年3月16日 上午10:06:08
 */
@Controller
public class GoodsAction {

	private Logger logger = Logger.getLogger(GoodsAction.class);

	@Resource
	private ParamValidateService paramValidateService;
	@Resource
	private GoodsService goodsService;
	@Resource
	private AreaService areaService;
	@Resource
	private ConfDataCacheService cdc;

	/**
	 * 
	 * getExportTheGoodAnalyze(出口商品定向分析) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param exportGoodsModel
	 * @param model
	 * @return
	 * @exception
	 * @since 2016年3月24日 上午8:54:13
	 * @author lougf
	 */
	@RequestMapping("export/thegood")
	@Access(FunctionsEnum.EXPORT_THEGOOD)
	public String getExportTheGoodAnalyze(
			@ModelAttribute("goodsModel") GoodsModel exportGoodsModel,
			Model model) {
		if (null == exportGoodsModel) {

			// 默认查近七天
			exportGoodsModel = new GoodsModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			exportGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			exportGoodsModel.setEndTime(today);
			model.addAttribute("default", true);
		}
		if (null != exportGoodsModel) {
			if (StringUtils.isBlank(exportGoodsModel.getStartTime())
					&& StringUtils.isBlank(exportGoodsModel.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				exportGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				exportGoodsModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(exportGoodsModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}
		model.addAttribute("countryMap", cdc.getCountryMap());
		return "export/thegood";

	}

	/**
	 * 
	 * getImportTheGoodAnalyze(进口商品定向分析) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param importGoodsModel
	 * @param model
	 * @return
	 * @exception
	 * @since 2016年3月24日 上午8:54:50
	 * @author lougf
	 */
	@RequestMapping("import/thegood")
	@Access(FunctionsEnum.IMPORT_THEGOOD)
	public String getImportTheGood(
			@ModelAttribute("importGoodsModel") GoodsModel importGoodsModel,
			Model model) {
		if (null == importGoodsModel) {

			// 默认查近七天
			importGoodsModel = new GoodsModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			importGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			importGoodsModel.setEndTime(today);
			model.addAttribute("default", true);
		}
		if (null != importGoodsModel) {
			if (StringUtils.isBlank(importGoodsModel.getStartTime())
					&& StringUtils.isBlank(importGoodsModel.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				importGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				importGoodsModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(importGoodsModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}
		return "import/thegood";

	}

	@RequestMapping("import/goodAnalyze")
	public String getImportTheGoodAnalyze(
			@ModelAttribute("importGoodsModel") GoodsModel importGoodsModel,
			Model model) {
		if (null == importGoodsModel) {

			// 默认查近七天
			importGoodsModel = new GoodsModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			importGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			importGoodsModel.setEndTime(today);
			model.addAttribute("default", true);
		}
		if (null != importGoodsModel) {
			if (StringUtils.isBlank(importGoodsModel.getStartTime())
					&& StringUtils.isBlank(importGoodsModel.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				importGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				importGoodsModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(importGoodsModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}
		Date starttime = DateUtils.getDate(importGoodsModel.getStartTime(),
				"yyyy-MM-dd");
		Date endtime = DateUtils.getDate(importGoodsModel.getEndTime(),
				"yyyy-MM-dd");

		Integer days = DateUtils.daysBetween(starttime, endtime);
		if (days > 150) {
			importGoodsModel.setStartTime_F(DateUtils.format(starttime,
					"yyyy-MM"));
			importGoodsModel.setEndTime_F(DateUtils.format(endtime, "yyyy-MM"));
			importGoodsModel.setStartTimePattern("yyyy-MM");
		}
		// 第二步：进行数据库查询
		// 根据省份查询销售数量，客单价排名
		List<AnaImportData> datas2 = goodsService.getGoodsImortDataByDateFlag(
				importGoodsModel, importGoodsModel.getProvince(),
				importGoodsModel.getKeywordCode());

		Collections.reverse(datas2);
		model.addAttribute("datas2",
				generateZeroDateByTime(importGoodsModel, datas2));
		// 计算均价(总金额除以总数量)
		model.addAttribute("avgprice", "0");
		model.addAttribute("sumnum", "0");
		model.addAttribute("scale", "0");
		if (null != datas2 && datas2.size() > 0) {
			BigDecimal allSumNum = new BigDecimal(new Double("0"));
			BigDecimal allSumPrice = new BigDecimal(new Double("0"));
			for (AnaImportData data : datas2) {
				if (null != data.getSumPrice()) {
					allSumPrice = allSumPrice.add(data.getSumPrice());
				}
				if (null != data.getSumNum()) {
					allSumNum = allSumNum.add(data.getSumNum());
				}
			}
			model.addAttribute("sumnum", allSumNum.toString());
			model.addAttribute("avgprice", String.valueOf(NumberUtil.div(
					allSumPrice.doubleValue(), allSumNum.doubleValue(), 2)));
			Long sumNum = goodsService.getAllProvinceSumNum(importGoodsModel,
					importGoodsModel.getKeywordCode());
			model.addAttribute("scale", NumberUtil.mul(
					NumberUtil.div(
							allSumNum.doubleValue(),
							new Double(String.valueOf(null == sumNum
									? 0l
										: sumNum)), 4), new Double("100")));

		}
		// 根据省份查询来源国排名
		List<AnaImportData> datas3 = goodsService.getGoodsImortDataByCountry(
				importGoodsModel, importGoodsModel.getProvince(),
				importGoodsModel.getKeywordCode());
		areaService
				.initImportDataMainName(datas3, DimensionFlag.EXPORT_COUNTRY);
		// 根据省份查询品类排名
		List<AnaImportData> datas4 = goodsService.getGoodsImortDataByCategory(
				importGoodsModel, importGoodsModel.getProvince(),
				importGoodsModel.getKeywordCode());
		goodsService.initImportDataMainName(datas4, DimensionFlag.S_CATEGORY);
		model.addAttribute("datas3", datas3);
		model.addAttribute("datas4", datas4);

		// 计算平均购买次数 男女比例 平均购买单价
		model.addAttribute("avgcount", "0");
		List<AnaImportData> datas5 = goodsService.getGoodsImortDataBySex(
				importGoodsModel, importGoodsModel.getProvince(),
				importGoodsModel.getKeywordCode());
		model.addAttribute("womanscale", "0");
		model.addAttribute("manscale", "0");
		if (null != datas5 && datas5.size() > 0) {
			BigDecimal allCountNum = new BigDecimal(new Double("0"));
			BigDecimal allSumNum = new BigDecimal(new Double("0"));
			BigDecimal manCountNum = new BigDecimal(new Double("0"));
			BigDecimal womanCountNum = new BigDecimal(new Double("0"));
			for (AnaImportData data : datas5) {
				if (null != data.getMainCodeCount()) {
					allCountNum = allCountNum.add(data.getMainCodeCount());
				}
				if (null != data.getSumNum()) {
					allSumNum = allSumNum.add(data.getSumNum());
				}
				if (data.getSecondCode().endsWith(SexEnum.M.name())) {
					manCountNum = data.getMainCodeCount();
				}
				else if (data.getSecondCode().endsWith(SexEnum.F.name())) {
					womanCountNum = data.getMainCodeCount();
				}
			}
			model.addAttribute("womanscale", NumberUtil.mul(
					NumberUtil.div(womanCountNum.doubleValue(),
							allCountNum.doubleValue(), 2), new Double("100")));
			model.addAttribute("manscale", NumberUtil.mul(
					NumberUtil.div(manCountNum.doubleValue(),
							allCountNum.doubleValue(), 2), new Double("100")));
			model.addAttribute("avgcount", String.valueOf(NumberUtil.div(
					allSumNum.doubleValue(), allCountNum.doubleValue(), 2)));

		}
		// 根据生日计算年龄分布
		List<AnaImportData> datas6 = goodsService.getGoodsImortDataByAge(
				importGoodsModel, importGoodsModel.getProvince(),
				importGoodsModel.getKeywordCode());
		Long before19 = 0l;// 18岁以下
		Long before26up18 = 0l;// 18岁以上26以下
		Long before36up25 = 0l;// 25岁以上35以下
		Long before46up35 = 0l;// 35岁以上46以下
		Long up45 = 0l;// 45岁以上
		Long max = 5l;
		if (null != datas6 && datas6.size() > 0) {
			Integer nowyear = Integer.parseInt(DateUtils.format("yyyy"));
			for (AnaImportData data : datas6) {
				try {
					Integer year = nowyear
							- Integer.parseInt(data.getSecondCode());
					if (year <= 18) {
						before19 = before19
								+ data.getMainCodeCount().longValue();
					}
					else if (year >= 19 && year < 26) {
						before26up18 = before26up18
								+ data.getMainCodeCount().longValue();

					}
					else if (year >= 26 && year < 36) {
						before36up25 = before36up25
								+ data.getMainCodeCount().longValue();

					}
					else if (year >= 36 && year < 46) {
						before46up35 = before46up35
								+ data.getMainCodeCount().longValue();

					}
					else {
						up45 = up45 + data.getMainCodeCount().longValue();

					}
				}
				catch (Exception e) {

				}
			}
		}

		model.addAttribute("before19", before19);
		model.addAttribute("before26up18", before26up18);
		model.addAttribute("before36up25", before36up25);
		model.addAttribute("before46up35", before46up35);
		model.addAttribute("up45", up45);
		model.addAttribute("max", max + before19 + before26up18 + before36up25
				+ before46up35 + up45);
		// 获取最多年龄段
		model.addAttribute("mostmax", NumberUtil.searchMaxWithIndex(new Long[] {
				before19, before26up18, before36up25, before46up35, up45
		})[0]);
		// 第三步：组装数据
		JSONArray result = new JSONArray();
		areaService.toJsonArrayImport(datas3, result, OrderByEnum.SUM_NUM);
		model.addAttribute("htmlText",
				result.toJSONString().replaceAll("\"name\"", "name")
						.replaceAll("\"value\"", "value")
						.replaceAll("\"", "\'"));
		// 地图最大值
		model.addAttribute("levelvalue", null != datas3 && datas3.size() > 0
				? (datas3.get(0).getSumNum().intValue() + 10000)
					: 10000);
		model.addAttribute("keywordCode", importGoodsModel.getKeywordCode());
		model.addAttribute("province", importGoodsModel.getProvince());
		model.addAttribute("days", days);
		// 判断是否最近日期
		model.addAttribute("isNow",
				DateUtils.format(importGoodsModel.getStartTimePattern())
						.equals(importGoodsModel.getEndTime_F()));
		model.addAttribute("startTime_F", importGoodsModel.getStartTime_F());
		model.addAttribute("endTime_F", importGoodsModel.getEndTime_F());

		return "import/goodAnalyze";

	}

	@RequestMapping("export/goodAnalyze")
	public String exportGoodAnalyze(
			@ModelAttribute("goodsModel") GoodsModel goodsModel, Model model) {
		if (null == goodsModel) {

			// 默认查近七天
			goodsModel = new GoodsModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			goodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(today, 6));
			goodsModel.setEndTime(today);
			model.addAttribute("default", true);
		}
		if (null != goodsModel) {
			if (StringUtils.isBlank(goodsModel.getStartTime())
					&& StringUtils.isBlank(goodsModel.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				goodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(today,
						6));
				goodsModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(goodsModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}
		Date starttime = DateUtils.getDate(goodsModel.getStartTime(),
				"yyyy-MM-dd");
		Date endtime = DateUtils.getDate(goodsModel.getEndTime(), "yyyy-MM-dd");

		Integer days = DateUtils.daysBetween(starttime, endtime);
		if (days > 150) {
			goodsModel.setStartTime_F(DateUtils.format(starttime, "yyyy-MM"));
			goodsModel.setEndTime_F(DateUtils.format(endtime, "yyyy-MM"));
			goodsModel.setStartTimePattern("yyyy-MM");
		}
		// 第二步：进行数据库查询
		// 根据国家查询销售数量，客单价排名
		List<AnaExportData> datas2 = goodsService.getGoodsExportDataByDateFlag(
				goodsModel, goodsModel.getCountry(),
				goodsModel.getKeywordCode());

		Collections.reverse(datas2);
		if (null != datas2) {
			for (AnaExportData data : datas2) {
				data.setAvgPrice(new BigDecimal(NumberUtil.round(data
						.getAvgPrice().doubleValue(), 2)));
			}
		}
		model.addAttribute("datas2",
				generateExportZeroDateByTime(goodsModel, datas2));
		// 计算均价(总金额除以总数量)
		model.addAttribute("avgprice", "0");
		model.addAttribute("sumnum", "0");
		model.addAttribute("scale", "0");
		if (null != datas2 && datas2.size() > 0) {
			BigDecimal allSumNum = new BigDecimal(new Double("0"));
			BigDecimal allSumPrice = new BigDecimal(new Double("0"));
			for (AnaExportData data : datas2) {
				if (null != data.getSumPrice()) {
					allSumPrice = allSumPrice.add(data.getSumPrice());
				}
				if (null != data.getSumNum()) {
					allSumNum = allSumNum.add(data.getSumNum());
				}
			}
			model.addAttribute("sumnum", allSumNum.toString());
			model.addAttribute("avgprice", String.valueOf(NumberUtil.div(
					allSumPrice.doubleValue(), allSumNum.doubleValue(), 2)));
			Long sumNum = goodsService.getAllExportCountrySumNum(goodsModel,
					goodsModel.getKeywordCode());
			model.addAttribute("scale", NumberUtil.mul(
					NumberUtil.div(
							allSumNum.doubleValue(),
							new Double(String.valueOf(null == sumNum
									? 0l
										: sumNum)), 4), new Double("100")));

		}
		// 根据商品查询出口国排名
		List<AnaExportData> datas3 = goodsService.getGoodsExportDataByCountry(
				goodsModel, goodsModel.getKeywordCode());
		areaService
				.initExportDataMainName(datas3, DimensionFlag.EXPORT_COUNTRY);

		model.addAttribute("datas3", datas3);
		// 第三步：组装数据
		JSONArray result = new JSONArray();
		areaService.toJsonArrayExport(datas3, result, OrderByEnum.SUM_NUM);
		model.addAttribute("htmlText",
				result.toJSONString().replaceAll("\"name\"", "name")
						.replaceAll("\"value\"", "value")
						.replaceAll("\"", "\'"));
		// 地图最大值
		model.addAttribute("levelvalue", null != datas3 && datas3.size() > 0
				? (datas3.get(0).getSumNum().intValue() + 10000)
					: 10000);
		model.addAttribute("keywordCode", goodsModel.getKeywordCode());
		if (!StringUtils.isEmpty(goodsModel.getCountry())) {

			model.addAttribute("country",
					cdc.getCountry(goodsModel.getCountry()).getName());
		}
		else {

			model.addAttribute("country", "");
		}
		model.addAttribute("days", days);
		// 判断是否最近日期
		model.addAttribute(
				"isNow",
				DateUtils.format(goodsModel.getStartTimePattern()).equals(
						goodsModel.getEndTime_F()));
		model.addAttribute("startTime_F", goodsModel.getStartTime_F());
		model.addAttribute("endTime_F", goodsModel.getEndTime_F());
		return "export/goodAnalyze";

	}

	@RequestMapping(value = "export/getGoodsByCountry", produces = "application/json;charset=UTF-8")
	public @ResponseBody void getGoodsByCountry(
			@RequestParam(value = "jsoncallback", required = false) String jsoncallback,
			HttpServletResponse response)
	{

		//真实日期
//		Date date = new Date();
//		String today = DateUtils.format(date, "yyyy-MM-dd");
//		Calendar cale = Calendar.getInstance();
//		String firstday;
//		cale.add(Calendar.MONTH, 0);
//		cale.set(Calendar.DAY_OF_MONTH, 1);
//		firstday = DateUtils.format(cale.getTime(), "yyyy-MM-dd");

		//假日期
//		String firstday = "2016-01-01";
//		String today = "2016-10-31";
		JSONObject result = new JSONObject();
//		List<Map<String, Object>> goodsCategoriesMList = goodsService.goodsCategoriesMList(firstday, today);
		List<Map<String, Object>> goodsCategoriesMList = goodsService.goodsCategoriesMList(null, null);
		result.put("goodsCategoriesMList", goodsCategoriesMList);
		Map<String, List<Map<String, Integer>>> goodsCategoriesByCountryMList = new HashMap<String, List<Map<String, Integer>>>();
		for(Map<String, Object> goodsCategoriesMap :goodsCategoriesMList){
//			goodsCategoriesByCountryMList.put(goodsCategoriesMap.get("MAIN_CODE") + "",
//					goodsService.goodsCategoriesByCountryMList(firstday, today, goodsCategoriesMap.get("MAIN_CODE")+""));
			goodsCategoriesByCountryMList.put(goodsCategoriesMap.get("MAIN_CODE") + "",
					goodsService.goodsCategoriesByCountryMList(null, null, goodsCategoriesMap.get("MAIN_CODE")+""));
		}
		result.put("goodsCategoriesByCountryMList",goodsCategoriesByCountryMList);
		//ajax跨境访问
		try {
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "export/getCountrycategories", produces = "application/json;charset=UTF-8")
	public @ResponseBody void getCountrycategories(
			@RequestParam(value = "jsoncallback", required = false) String jsoncallback,
			HttpServletResponse response)
	{

		//真实日期
//		Date date = new Date();
//		String today = DateUtils.format(date, "yyyy-MM-dd");
//		Calendar cale = Calendar.getInstance();
//		String firstday;
//		cale.add(Calendar.MONTH, 0);
//		cale.set(Calendar.DAY_OF_MONTH, 1);
//		firstday = DateUtils.format(cale.getTime(), "yyyy-MM-dd");

		//假日期
//		String firstday = "2016-01-01";
//		String today = "2016-10-31";
		int topN = 3;
		JSONObject result = new JSONObject();
//		List<Map<String, Object>> topCountryList = goodsService.getTopCountryNameList(firstday, today, topN);
		List<Map<String, Object>> topCountryList = goodsService.getTopCountryNameList(null, null, topN);
		List<Map<String, Object>> topCountryCategoriesList = new ArrayList<>();
		for(Map<String, Object> goodsCategoriesMap: topCountryList){
			Map<String, Object> item = new HashMap<>();
			item.put("countryName", goodsCategoriesMap.get("NAME") + "");
//			item.put("categoryList",
//					goodsService.getTopCountryCategoriesList(firstday, today, goodsCategoriesMap.get("COUNTRY_CODE")+"", 9));
//			item.put("categoryList",
//					goodsService.getTopCountryCategoriesList("2017-11-01", "2017-12-30", goodsCategoriesMap.get("COUNTRY_CODE")+"", 9));
            item.put("categoryList",
                    goodsService.getTopCountryCategoriesList(null, null, goodsCategoriesMap.get("COUNTRY_CODE")+"", 9));
			topCountryCategoriesList.add(item);
		}
		result.put("goodsCategoriesByCountryMList",topCountryCategoriesList);
		//ajax跨境访问
		try {
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


    @RequestMapping(value = "export/getTopCountryNameList", produces = "application/json;charset=UTF-8")
    public @ResponseBody void getTopCountryNameList(
            @RequestParam(value = "jsoncallback", required = false) String jsoncallback,
            HttpServletResponse response)
    {
        //真实日期
//        Date date = new Date();
//        String today = DateUtils.format(date, "yyyy-MM-dd");
//        Calendar cale = Calendar.getInstance();
//        String firstday;
//        cale.add(Calendar.MONTH, 0);
//        cale.set(Calendar.DAY_OF_MONTH, 1);
//        firstday = DateUtils.format(cale.getTime(), "yyyy-MM-dd");

        JSONObject result = new JSONObject();
//        List<String> topCategoryList = goodsService.getTopCategoryList(firstday, today, 8);
		List<String> topCategoryList = goodsService.getTopCategoryList(null, null, 8);
        result.put("topCategoryList", topCategoryList);

        //ajax跨境访问
        try {
            response.setContentType("text/json;charset=UTF-8");
            response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	@RequestMapping(value = "export/getAllSum", produces = "application/json;charset=UTF-8")
	public @ResponseBody void getAllSum(
			@RequestParam(value = "jsoncallback", required = false) String jsoncallback,
			HttpServletResponse response)
	{
		JSONObject result = new JSONObject();
		Integer countryNum = goodsService.getAllCountrySum();
		Map<String, Object> salesCount = goodsService.getSumSalesCount();
		Map<String, Object> salesCountDH = goodsService.getDHSumSalesCount();
		result.put("countryNum", countryNum);
		result.put("salesCount", salesCount);
		result.put("salesCountDH", salesCountDH);
		//ajax跨境访问
		try {
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "export/getWorld3Ddata", produces = "application/json;charset=UTF-8")
	public @ResponseBody void getWorld3Ddata(
			@RequestParam(value = "jsoncallback", required = false) String jsoncallback,
			HttpServletResponse response)
	{

		//真实日期
//		Date date = new Date();
//		String today = DateUtils.format(date, "yyyy-MM-dd");
//		Calendar cale = Calendar.getInstance();
//		String firstday;
//		cale.add(Calendar.MONTH, 0);
//		cale.set(Calendar.DAY_OF_MONTH, 1);
//		firstday = DateUtils.format(cale.getTime(), "yyyy-MM-dd");

		//假日期
//		String firstday = "2016-01-01";
//		String today = "2016-10-31";
		int topN = -1;
		JSONObject result = new JSONObject();
//		List<Map<String, Object>> CountryList = goodsService.getTopCountryNameList(firstday, today, topN);
//		List<String> topCategoryList = goodsService.getTopCategoryList(firstday, today, 8);
		List<Map<String, Object>> CountryList = goodsService.getTopCountryNameList(null, null, topN);
		List<String> topCategoryList = goodsService.getTopCategoryList(null, null, 8);
		Map<String, Object> CountryCategoriesList = new HashMap<>();
		List<Map<String, Object>> CountryValue = new ArrayList<>();
		CountryCategoriesList.put("years", topCategoryList);

		for(Map<String, Object> Country: CountryList){
			Map<String, Object> CountryValueItem = new HashMap<>();
			CountryValueItem.put("name", Country.get("NAME"));
			CountryValueItem.put("value", Country.get("VALUE"));
			CountryValue.add(CountryValueItem);
		}

		List<Map<String,Object>> originCountryCategory = goodsService.getAllCountryCategory(topCategoryList);

		for(Map<String, Object> csi: originCountryCategory){
			List<Integer> sumnumItem = new ArrayList<>();
			for(int i=0; i<topCategoryList.size(); i++){
				sumnumItem.add(0);
			}
			String[] categoryValur = csi.get("DATA_LIST").toString().split("\\,");
			for(int i=0; i<categoryValur.length; i+=2){
				int index = topCategoryList.indexOf(categoryValur[i]);
				if(index!=-1){
					sumnumItem.set(index, Integer.valueOf(categoryValur[i+1]));
				}
			}
			CountryCategoriesList.put(csi.get("COUNTRY_NAME").toString(), sumnumItem);
		}
		
//		List<Integer> sumCategoryNum = goodsService.getSumCategoryNum(firstday, today, topCategoryList);
		List<Integer> sumCategoryNum = goodsService.getSumCategoryNum(null, null, topCategoryList);
		result.put("CountryCategoriesList", CountryCategoriesList);
		result.put("CountryValue", CountryValue);
        result.put("sumCategoryNum", sumCategoryNum);
		//ajax跨境访问
		try {
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

    @RequestMapping(value = "export/getCategoryMonthData", produces = "application/json;charset=UTF-8")
    public @ResponseBody void getCategoryMonthData(
            @RequestParam(value = "jsoncallback", required = false) String jsoncallback, HttpServletRequest request,
            HttpServletResponse response, int monthN, String categoryName)
    {
        try {
            categoryName = new String(request.getParameter("categoryName").getBytes("ISO-8859-1"),"utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        JSONObject result = new JSONObject();
        List<String> dateList = new ArrayList<>();
        Date date = new Date();
        String dateString = DateUtils.format(date, "yyyy-MM");
        dateList.add(dateString);
        for(int i=1; i<monthN;i++){
            Calendar cale = Calendar.getInstance();
            cale.add(Calendar.MONTH, -i);
            dateString = DateUtils.format(cale.getTime(), "yyyy-MM");
            dateList.add(dateString);
        }
        List<Map<String, Object>> categoryMonthList = goodsService.getCategoryMonthData(monthN, categoryName);
		List<BigDecimal> realCategoryMonthList = new ArrayList<BigDecimal>();
		for(String d: dateList){
			BigDecimal num = null;
			for(Map<String, Object> item:categoryMonthList){
				if(item.get("DATE_FLAG").equals(d)){
					num = (BigDecimal)item.get("SUM_NUM");
				}
			}
			realCategoryMonthList.add(num==null?new BigDecimal("0"):num);
		}
        result.put("dateList", dateList);
        result.put("categoryMonthList", realCategoryMonthList);
        //ajax跨境访问
        try {
            response.setContentType("text/json;charset=UTF-8");
            response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "export/getGoodRangeData", produces = "application/json;charset=UTF-8")
    public @ResponseBody void getGoodRangeData(
            @RequestParam(value = "jsoncallback", required = false) String jsoncallback,
            HttpServletResponse response)
    {
        int topN = 10;
        JSONObject result = new JSONObject();
        List<Map<String, Object>> goodRangeList = goodsService.getGoodRangeData(topN);
        result.put("goodRangeList", goodRangeList);
        //ajax跨境访问
        try {
            response.setContentType("text/json;charset=UTF-8");
            response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

	@ResponseBody
	@RequestMapping(value = "import/titles", method = RequestMethod.POST)
	public List<String> titles(String keyword) {
		List<String> titleList = goodsService.getItemNameListByKeyword(keyword);
		return null == titleList ? new ArrayList<String>() : titleList;
	}

	@ResponseBody
	@RequestMapping(value = "export/titles", method = RequestMethod.POST)
	public List<String> exporttitles(String keyword) {
		List<String> titleList = goodsService
				.getExportItemNameListByKeyword(keyword);
		return null == titleList ? new ArrayList<String>() : titleList;

	}

	@RequestMapping(value = "export/countryAndGoods", produces = "application/json;charset=UTF-8")
	public @ResponseBody void countryAndGoods(
			@RequestParam(value = "jsoncallback", required = false) String jsoncallback,
			HttpServletResponse response)
	{
		GoodsModel exportGoodsModel = new GoodsModel();
		Date date = new Date();
		String today = DateUtils.format(date, "yyyy-MM-dd");
		Calendar cale = Calendar.getInstance();
		String firstday;
		cale.add(Calendar.MONTH, 0);
		cale.set(Calendar.DAY_OF_MONTH, 1);
		firstday = DateUtils.format(cale.getTime(), "yyyy-MM-dd");
		exportGoodsModel.setStartTime(firstday);
		exportGoodsModel.setEndTime(today);
		exportGoodsModel.setStartTime_F(firstday);
		exportGoodsModel.setEndTime_F(today);
		JSONObject result = new JSONObject();
		// 单品销售数量排名
		List<Ana2ndData> datas1 = goodsService.getTopNGoodsData(
				exportGoodsModel, 10, OrderByEnum.SUM_NUM,
				DimensionFlag.GOODS_TYPE);
		goodsService.initDataMainName(datas1, DimensionFlag.GOODS_TYPE);
		// 品类销售数量排名
		List<Ana2ndData> datas2 = goodsService.getTopNGoodsData(
				exportGoodsModel, 20, OrderByEnum.SUM_NUM,
				DimensionFlag.S_CATEGORY);
		goodsService.initDataMainName(datas2, DimensionFlag.S_CATEGORY);

		// 国家销售数量排名
		List<Ana2ndData> datas3 = areaService.getTopNAreaData(exportGoodsModel,
				300, OrderByEnum.SUM_NUM, DimensionFlag.EXPORT_COUNTRY);
		areaService.initDataMainName(datas3, DimensionFlag.EXPORT_COUNTRY);
		Collections.reverse(datas1);

		//按时间统计商品大类销量排名
		List<Ana2ndData> datas4 = goodsService.getMcategoryByDateFlag(exportGoodsModel);

		//在全部数据中统计 亚洲 美洲 欧洲 top10 的商品销量
        String datas5 = goodsService.getContinentTop10();
        JSONObject jsonObject = new JSONObject();
        jsonObject = JSON.parseObject(datas5);
        datas5 = jsonObject.toJSONString();

		// 第三步：组装数据
		JSONArray area = new JSONArray();
		areaService.toJsonArray(datas3, area, OrderByEnum.SUM_NUM);
		result.put("datas1", datas1);
		result.put("datas2", datas2);
		result.put("datas3", datas3);
		result.put("datas4", datas4);
		result.put("datas5", jsonObject);
		result.put("htmlTest",area.toJSONString().replaceAll("\"name\"", "name")
				.replaceAll("\"value\"", "value")
				.replaceAll("\"", "\'"));
		result.put("levelvalue", null != datas3 && datas3.size() > 0
				? (datas3.get(0).getSumNum().intValue() + 10000)
				: 10000);
		//ajax跨境访问
		try {
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	//获取当日、当月、当年的总票数
	@RequestMapping(value = "export/getDMYsumPkg", produces = "application/json;charset=UTF-8")
	public @ResponseBody void getDMYsumPkg(
			@RequestParam(value = "jsoncallback", required = false) String jsoncallback,
			HttpServletResponse response)
	{
		Date dNow = new Date();   //当前时间
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance(); //得到日历
		calendar.setTime(dNow);//把当前时间赋给日历
		int day = calendar.get(Calendar.DATE);
//		calendar.set(Calendar.DATE,day-1);
		calendar.set(Calendar.DATE,day);
		dBefore = calendar.getTime();

		JSONObject result = new JSONObject();

		Integer coustomerNum = goodsService.getCoustomerNum();
		result.put("coustomerNum", coustomerNum == null ? 0 : coustomerNum);

//		String yesterday = new SimpleDateFormat("yyyy-MM-dd").format(dBefore);
//		Integer d = goodsService.getYesterdayCount(yesterday);
		String today = new SimpleDateFormat("yyyy-MM-dd").format(dNow);
		Integer d = goodsService.getYesterdayCount(today);
		result.put("yesterdayCount", d == null ? 0 : d);

		String thisMonth = new SimpleDateFormat("yyyy-MM").format(dNow);
		Integer m = goodsService.getThisMonthCount(thisMonth);
		result.put("thisMonthCount", m == null ? 0 : m);

		String thisYear = new SimpleDateFormat("yyyy").format(dNow);
		Integer y = goodsService.getThisYearCount(thisYear);
		result.put("thisYearCount", y == null ? 0 : y);
		//ajax跨境访问
		try {
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "export/getGoodsMData", produces = "application/json;charset=UTF-8")
	public @ResponseBody void getGoodsMData(
			@RequestParam(value = "jsoncallback", required = false) String jsoncallback,
			int month, HttpServletResponse response)
	{
		JSONObject result = new JSONObject();
		result.put("goodsMList", goodsService.getGoodsMData(month-1));
		//ajax跨境访问
		try {
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@RequestMapping(value = "export/getTBandHB", produces = "application/json;charset=UTF-8")
	public @ResponseBody void getTBandHB(
			@RequestParam(value = "jsoncallback", required = false) String jsoncallback,
			HttpServletResponse response)
	{
		Date dNow = new Date();
		Date dBefore = new Date();
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(dNow);
		calendar.add(calendar.MONTH, -12);
		dBefore = calendar.getTime();
		String date1 = DateUtils.format(dNow, "yyyy-MM");
		String date2 = DateUtils.format(dBefore, "yyyy-MM");
		calendar.setTime(dNow);
		calendar.add(calendar.MONTH, -1);
		dBefore = calendar.getTime();
		String date3 = DateUtils.format(dBefore, "yyyy-MM");
		JSONObject result = new JSONObject();
		List<Ana3rdData> list = goodsService.getTBandHB(date1, date2, date3);

		if(list.get(2).getSumNum().toString().equals("0")){
			result.put("TB", 0);
		}else{
			result.put("TB", ((list.get(0).getSumNum().subtract(list.get(2).getSumNum())).divide(list.get(2).getSumNum(),
					4, BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal("100")));
		}

		if(list.get(1).getSumNum().toString().equals("0")){
			result.put("HB", 0);
		}else{
			result.put("HB", ((list.get(0).getSumNum().subtract(list.get(1).getSumNum())).divide(list.get(1).getSumNum(),
					4, BigDecimal.ROUND_HALF_UP)).multiply(new BigDecimal("100")));
		}

		//ajax跨境访问
		try {
			response.setContentType("text/json;charset=UTF-8");
			response.getWriter().write(jsoncallback+"("+result.toJSONString()+")");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 出口-商品分析 getImportGoodsAnalyze(这里用一句话描述这个方法的作用) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param exportGoodsModel
	 * @param model
	 * @return
	 * @throws
	 * @author lougf
	 * @since 2016-3-11 下午1:57:55
	 */
	@RequestMapping("export/goods")
	@Access(FunctionsEnum.EXPORT_GOODS)
	public String getExportGoodsAnalyze(
			@ModelAttribute("goodsModel") GoodsModel exportGoodsModel,
			Model model) {
		if (null == exportGoodsModel) {

			// 默认查近七天
			exportGoodsModel = new GoodsModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			exportGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			exportGoodsModel.setEndTime(today);
			model.addAttribute("default", true);
		}
		if (null != exportGoodsModel) {
			if (StringUtils.isBlank(exportGoodsModel.getStartTime())
					&& StringUtils.isBlank(exportGoodsModel.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				exportGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				exportGoodsModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(exportGoodsModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}
		Date starttime = DateUtils.getDate(exportGoodsModel.getStartTime(),
				"yyyy-MM-dd");
		Date endtime = DateUtils.getDate(exportGoodsModel.getEndTime(),
				"yyyy-MM-dd");

		Integer days = DateUtils.daysBetween(starttime, endtime);
		if (days > 150) {
			exportGoodsModel.setStartTime_F(DateUtils.format(starttime,
					"yyyy-MM"));
			exportGoodsModel.setEndTime_F(DateUtils.format(endtime, "yyyy-MM"));
			exportGoodsModel.setStartTimePattern("yyyy-MM");
		}

		// 第二步：进行数据库查询
		// 单品销售数量排名
		List<Ana2ndData> datas1 = goodsService.getTopNGoodsData(
				exportGoodsModel, 10, OrderByEnum.SUM_NUM,
				DimensionFlag.GOODS_TYPE);
		goodsService.initDataMainName(datas1, DimensionFlag.GOODS_TYPE);
		// 品类销售数量排名
//		List<Ana2ndData> datas2 = goodsService.getTopNGoodsData(
//				exportGoodsModel, 10, OrderByEnum.SUM_NUM,
//				DimensionFlag.S_CATEGORY);
		List<Map<String, Object>> temp = goodsService.getTopCategorySumList(DateUtils.format(starttime, "yyyy-MM-dd"),
				DateUtils.format(endtime, "yyyy-MM-dd"), 10);
		//goodsService.initDataMainName(datas2, DimensionFlag.S_CATEGORY);
		List<Ana2ndData> datas2 = new ArrayList<Ana2ndData>();
		for(Map<String, Object> item:temp){
			Ana2ndData ana = new Ana2ndData();
			ana.setMainName(item.get("MAIN_NAME").toString());
			ana.setSumNum(new BigDecimal(item.get("SUM_NUM").toString()));
			datas2.add(ana);
		}

		// 国家销售数量排名
		List<Ana2ndData> datas3 = areaService.getTopNAreaData(exportGoodsModel,
				300, OrderByEnum.SUM_NUM, DimensionFlag.EXPORT_COUNTRY);
			areaService.initDataMainName(datas3, DimensionFlag.EXPORT_COUNTRY);
		Collections.reverse(datas1);
		//临时-假数据
//		List<String> fakeClassification = new ArrayList<String>();
//		fakeClassification.add("儿童读物");
//		fakeClassification.add("灯具");
//		fakeClassification.add("乐器");
//		fakeClassification.add("图书音响");
//		fakeClassification.add("文化旅游");
//		fakeClassification.add("办公用品");
//		fakeClassification.add("家具");
//		fakeClassification.add("工艺品");
//		fakeClassification.add("玩具");
//		fakeClassification.add("饰品");
//		if(datas1.size()>0){
//			for(int i=0; i<10; i++){
//				datas1.get(i).setMainName(fakeClassification.get(i));
//			}
//		}
//		datas1.remove(10);
		model.addAttribute("datas1", datas1);
		model.addAttribute("datas2", datas2);
		model.addAttribute("datas3", datas3);

		// 新增需求开始
		goodsService.updateModel(exportGoodsModel);
		ExportGoodsDetailVo goodsDetailVo = goodsService.getTopOneData(
				exportGoodsModel, datas3);
		if (null != goodsDetailVo) {
			model.addAttribute("goodsPage", goodsDetailVo.getPage());
			//
			JSONObject goodsJsonObject=new JSONObject();
			
			goodsService.setGoodsJsonObject(exportGoodsModel,goodsDetailVo,goodsJsonObject);
			model.addAttribute("goodsChartJson", goodsJsonObject.toJSONString());
		}
		
		// 新增需求结束

		// 第三步：组装数据
		JSONArray result = new JSONArray();
		areaService.toJsonArray(datas3, result, OrderByEnum.SUM_NUM);
		model.addAttribute("htmlText",
				result.toJSONString().replaceAll("\"name\"", "name")
						.replaceAll("\"value\"", "value")
						.replaceAll("\"", "\'"));
		// 地图最大值
		model.addAttribute("levelvalue", null != datas3 && datas3.size() > 0
				? (datas3.get(0).getSumNum().intValue() + 10000)
					: 10000);
		model.addAttribute("yes", exportGoodsModel.getSelectButton());
		model.addAttribute("days", days);
		return "export/goods";
	}
	@RequestMapping("export/counntrygoods")
	public void getCountryGoodsJson(GoodsModel exportGoodsModel,GoodsVo vo,Page page,PrintWriter out){
		if (null == exportGoodsModel) {

			// 默认查近七天
			exportGoodsModel = new GoodsModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			exportGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			exportGoodsModel.setEndTime(today);
			
		}
		if (null != exportGoodsModel) {
			if (StringUtils.isBlank(exportGoodsModel.getStartTime())
					&& StringUtils.isBlank(exportGoodsModel.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				exportGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				exportGoodsModel.setEndTime(today);
				
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(exportGoodsModel)) {
			logger.info("error param!");
			JSONObject goodsJsonObject=new JSONObject();
			goodsJsonObject.put("error", true);
			out.print( goodsJsonObject.toJSONString());
			return ;
		}
		goodsService.updateModel(exportGoodsModel);
		ExportGoodsDetailVo goodsDetailVo = goodsService.getTopOneData(
				exportGoodsModel, vo.getCountryCode(),vo.getGoodsName(),page);
		if (null != goodsDetailVo) {
			
			JSONObject goodsJsonObject=new JSONObject();
			
			goodsJsonObject.put("goodsPage", goodsDetailVo.getPage());
			goodsService.setGoodsJsonObject(exportGoodsModel,goodsDetailVo,goodsJsonObject);
			
			out.print( goodsJsonObject.toJSONString());
		}
	}
	/**
	 * 进口-商品分析 getImportGoodsAnalyze(这里用一句话描述这个方法的作用) <br>
	 * (这里描述这个方法适用条件 – 可选)
	 * 
	 * @param importGoodsModel
	 * @param model
	 * @return
	 * @throws
	 * @author lougf
	 * @since 2016-3-11 下午1:57:55
	 */
	@RequestMapping("import/goods")
	@Access(FunctionsEnum.IMPORT_GOODS)
	public String getImportGoodsAnalyze(
			@ModelAttribute("goodsModel") GoodsModel importGoodsModel,
			Model model) {
		if (null == importGoodsModel) {

			// 默认查近七天
			importGoodsModel = new GoodsModel();
			Date date = new Date();
			String today = DateUtils.format(date, "yyyy-MM-dd");
			importGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
					today, 6));
			importGoodsModel.setEndTime(today);
			model.addAttribute("default", true);
		}
		if (null != importGoodsModel) {
			if (StringUtils.isBlank(importGoodsModel.getStartTime())
					&& StringUtils.isBlank(importGoodsModel.getEndTime())) {
				// 默认查近七天

				Date date = new Date();
				String today = DateUtils.format(date, "yyyy-MM-dd");
				importGoodsModel.setStartTime(DateUtils.getSpecifiedDayBefore(
						today, 6));
				importGoodsModel.setEndTime(today);
				model.addAttribute("default", true);
			}
		}

		// 第一步：校验输入参数
		if (!paramValidateService.validate(importGoodsModel)) {
			logger.info("error param!");
			model.addAttribute("error", "error param!");
			return "error";
		}

		// 第二步：进行数据库查询
		// 单品销售数量排名
		List<AnaImportData> datas1 = goodsService.getTopNGoodsImortData(
				importGoodsModel, 10, OrderByEnum.SUM_NUM,
				DimensionFlag.GOODS_TYPE);
		goodsService.initImportDataMainName(datas1, DimensionFlag.GOODS_TYPE);
		// 品类销售数量排名
		List<AnaImportData> datas2 = goodsService.getTopNGoodsImortData(
				importGoodsModel, 10, OrderByEnum.SUM_NUM,
				DimensionFlag.S_CATEGORY);
		goodsService.initImportDataMainName(datas2, DimensionFlag.S_CATEGORY);

		// 国家销售数量排名
		List<AnaImportData> datas3 = areaService.getTopNAreaImortData(
				importGoodsModel, 300, OrderByEnum.SUM_NUM,
				DimensionFlag.EXPORT_COUNTRY);
		areaService
				.initImportDataMainName(datas3, DimensionFlag.EXPORT_COUNTRY);
		Collections.reverse(datas1);
		model.addAttribute("datas1", datas1);
		model.addAttribute("datas2", datas2);
		model.addAttribute("datas3", datas3);

		// 第三步：组装数据
		JSONArray result = new JSONArray();
		areaService.toJsonArrayImport(datas3, result, OrderByEnum.SUM_NUM);
		model.addAttribute("htmlText",
				result.toJSONString().replaceAll("\"name\"", "name")
						.replaceAll("\"value\"", "value")
						.replaceAll("\"", "\'"));
		// 地图最大值
		model.addAttribute("levelvalue", null != datas3 && datas3.size() > 0
				? (datas3.get(0).getSumNum().intValue() + 10000)
					: 10000);
		model.addAttribute("yes", importGoodsModel.getSelectButton());
		model.addAttribute("days", DateUtils.daysBetween(DateUtils.getDate(
				importGoodsModel.getStartTime(), "yyyy-MM-dd"), DateUtils
				.getDate(importGoodsModel.getEndTime(), "yyyy-MM-dd")));
		return "import/goods";
	}

	/**
	 * 时间区间无数据的补全0数据
	 */
	private List<AnaImportData> generateZeroDateByTime(BaseParamModel model,
			List<AnaImportData> data) {
		List<AnaImportData> newDate = new ArrayList<AnaImportData>();
		Map<String, AnaImportData> temp = new HashMap<String, AnaImportData>();
		if (null != data) {
			for (AnaImportData ana1stData : data) {
				temp.put(ana1stData.getDateFlag(), ana1stData);
			}
		}

		List<String> timeList = DateUtils.getDateList(model.getStartTime_F(),
				model.getEndTime_F());
		for (String time : timeList) {
			if (temp.containsKey(time)) {
				newDate.add(temp.get(time));
			}
			else {
				AnaImportData tempAna1stData = new AnaImportData();
				tempAna1stData.setDateFlag(time);
				tempAna1stData.setSumNum(new BigDecimal(0));
				tempAna1stData.setAvgPrice(new BigDecimal(0));
				newDate.add(tempAna1stData);

			}
		}
		return newDate;
	}

	/**
	 * 时间区间无数据的补全0数据
	 */
	private List<AnaExportData> generateExportZeroDateByTime(
			BaseParamModel model, List<AnaExportData> data) {
		List<AnaExportData> newDate = new ArrayList<AnaExportData>();
		Map<String, AnaExportData> temp = new HashMap<String, AnaExportData>();
		if (null != data) {
			for (AnaExportData ana1stData : data) {
				temp.put(ana1stData.getDateFlag(), ana1stData);
			}
		}

		List<String> timeList = DateUtils.getDateList(model.getStartTime_F(),
				model.getEndTime_F());
		for (String time : timeList) {
			if (temp.containsKey(time)) {
				newDate.add(temp.get(time));
			}
			else {
				AnaExportData tempAna1stData = new AnaExportData();
				tempAna1stData.setDateFlag(time);
				tempAna1stData.setSumNum(new BigDecimal(0));
				tempAna1stData.setAvgPrice(new BigDecimal(0));
				newDate.add(tempAna1stData);

			}
		}
		return newDate;
	}


}
